// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;

namespace ValidationAspects.Sdk
{
	public static class ValidatorFactoryExtensions
	{
		#region Inner
		private interface IValidatorFactoryInvoker
		{
			object Invoke(IValidatorFactory factory);
		}

		private class ValidatorFactoryInvoker<T> : IValidatorFactoryInvoker
		{
			#region Implementation of IValidatorFactoryInvoker
			public object Invoke(IValidatorFactory factory)
			{
				return factory.Create<T>();
			}
			#endregion
		}
		#endregion

		public static IValidator Create(this IValidatorFactory factory, Type type)
		{
			Enforce.IsNotNull(factory, "factory");
			Enforce.IsNotNull(type, "type");

			IValidatorFactoryInvoker factoryInvoker = (IValidatorFactoryInvoker)Activator.CreateInstance(typeof(ValidatorFactoryInvoker<>).MakeGenericType(type));
			return (IValidator)factoryInvoker.Invoke(factory);
		}
	}
}